<template>
  <view class="wrap">
    <view class="shipment-list" v-if="shipmentOrders.length">
      <view v-for="(order, index) in shipmentOrders" :key="index" class="card">
        <view class="card-header">
          <text class="title">发货单-{{index + 1}}</text>
          <text :class="['tag-view', getStatusClass(order.status)]">{{ order.statusName }}</text>
        </view>
        <view class="card-body">
          <view v-if="order.status == 1" class="empty-state">
            <image src="@/static/images/default.png" />
            <text>订单未发货，快马加鞭处理中...</text>
          </view>
          <view v-else>
            <view class="info-row">
              <text class="label">发货方式:</text>
              <text class="value">{{ order.shippingMethod }}</text>
            </view>
            <view class="info-row">
              <text class="label">物流/快递号:</text>
              <text class="value">{{ order.trackingNumber }}</text>
            </view>
            <view class="info-row">
              <text class="label">申请发货日期:</text>
              <text class="value">{{ order.requestDate }}</text>
            </view>
            <view class="info-row">
              <text class="label">实际发货日期:</text>
              <text class="value">{{ order.actualDate }}</text>
            </view>
            <view class="info-row">
              <text class="label">出库单号:</text>
              <text class="value">{{ order.deliveryNote }}</text>
            </view>
            <view class="related-parts-section" v-if="order.shipmentDetailList && order.shipmentDetailList.length">
              <view class="related-title">发货明细</view>
              <view class="related-parts-list">
                <view v-for="(item, itemIndex) in order.shipmentDetailList" :key="itemIndex" class="related-part-item">
                  <image v-if="item.imageUrl" @click="previewImage(item.imageUrl)" :src="item.imageUrl" class="part-image" />
                  <view v-else class="empty-image">
                    <uni-icons color="#999999" size="20" type="image"></uni-icons>
                    <view class="empty-image-text">暂无图片</view>
                  </view>
                  <view class="part-details">
                    <view class="item-name">{{ item.partName || '--' }}</view>
                    <view class="part-info-row">
                      <view class="label">规格型号:</view>
                      <view class="value">{{ item.model || '--' }}</view>
                    </view>
                    <view class="part-info-row">
                      <view class="label">物料编号:</view>
                      <view class="value">{{ item.partNumber || '--' }}</view>
                    </view>
                    <view class="part-info-row">
                      <view class="label">发货数量:</view>
                      <view class="value">{{ item.quantity || '--' }}{{item.unit || ''}}</view>
                    </view>
                  </view>
                </view>
              </view>
            </view>
          </view>
        </view>
      </view>
    </view>
    <view class="empty-box" v-else>
      <view class="empty-state">
        <image src="@/static/images/default.png" />
        <text>订单未发货，快马加鞭处理中...</text>
      </view>
    </view>
  </view>
</template>

<script>

import {getERPDetail} from '@/apis/parts';

export default {
	name: 'shipmentDetail',
	components: { },
	data() {
		return {
			orderId: '',
			shipmentOrders: [],
		};
	},
	onLoad(e) {
		if (e.orderId) {
			this.orderId = e.orderId;
		}
		this.getData();
	},
	methods: {
		// 图片预览
		previewImage(url) {
			console.log(url, '图片预览');
			if (!url) return;
			uni.previewImage({
				current: Array.isArray(url) ? url[0] : url,
				urls: Array.isArray(url) ? url : [url],
			});
		},

		async getData() {
			try {
				uni.showLoading({
					title: '加载中...',
				});
				const res = await getERPDetail({orderId: this.orderId});
				if (res && res.code === 200 && res.data) {
					let list = res.data[0].deliveryList || [];
					this.shipmentOrders = list.map(item => {
						if (item.imageUrl && Array.isArray(item.imageUrl)) {
							item.imageUrl = item.imageUrl[0]; // 取第一个图片链接
						}
						if (item.shipmentDetailList) {
							item.shipmentDetailList.forEach(v=>{
								if (v.quantity) {
									v.quantity = Number(v.quantity);
								}
							});
						}

						return item;
					});
				}
				uni.hideLoading();
			} catch (error) {
				uni.hideLoading();
				console.log(error);
			}
		},
 		getStatusClass(status) {
			switch (Number(status)) {
			case 3:
				return 'tag-view-red';
			case 2:
				return 'tag-view-blue';
			case 5:
				return 'tag-view-green';
			case 7:
				return 'tag-view-orange';
			default:
				return 'tag-view-gray';
			}
		},
	},
};
</script>

<style scoped lang="scss">
.empty-box{
  display: flex;
  align-items: center;
  justify-content: center;
  height: 60vh;
}
.wrap {
  background-color: #f3f6f8;
  min-height: 100vh;
}
.shipment-list {
  padding: 20rpx;
}
.card {
  background-color: #fff;
  border-radius: 8rpx;
  padding: 24rpx;
  margin-bottom: 20rpx;
  .card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    //padding-bottom: 20rpx;
    //border-bottom: 1px solid #f0f0f0;
    .title {
      font-size: 32rpx;
      font-weight: bold;
      color: #333;
    }
  }
  .card-body {
    padding-top: 20rpx;
    .info-row {
      display: flex;
      font-size: 28rpx;
      margin-bottom: 16rpx;
      &:last-child {
        margin-bottom: 0;
      }
      .label {
        color: #999;
        width: 190rpx;
      }
      .value {
        color: #333;
        flex: 1;
      }
    }
  }
}

.tag-view {
  padding: 0 12rpx;
  font-size: 24rpx;
  border-radius: 8rpx;
  border: 1px solid transparent;
  display: inline-block;
}
.tag-view-green {
  color: #16bf99;
  border-color: #16bf99;
}
.tag-view-orange {
  color: #fa6400;
  border-color: #fa6400;
}
.tag-view-red{
  color: #E02020;
  border: 1px solid #E02020;
}
.tag-view-blue{
  color: #0091FF;
  border: 1px solid #0091FF;
}
.tag-view-gray{
  color: #666666;
  border: 1px solid #666666;
}
.related-parts-section {
  background-color: #f8f8f8;
  border-radius: 8rpx;
  padding: 16rpx 24rpx;
  margin-top: 20rpx;
  .related-title {
    font-size: 28rpx;
    font-weight: bold;
    margin-bottom: 16rpx;
    color: #333;
    &:before {
      content: '';
      display: inline-block;
      width: 4rpx;
      height: 24rpx;
      background-color: #0091FF;
      margin-right: 8rpx;
      vertical-align: middle;
    }
  }
  .related-parts-list {
    .related-part-item {
      display: flex;
      padding: 16rpx 0;
      border-bottom: 1px solid #e9e9e9;
      &:first-child {
        padding-top: 16rpx;
      }
      &:last-child {
        border-bottom: none;
        padding-bottom: 0;
      }
      .part-image {
        width: 114rpx;
        height: 114rpx;
        margin-right: 16rpx;
        flex-shrink: 0;
        border-radius: 8rpx;
      }
      .empty-image{
        width: 114rpx;
        height: 114rpx;
        margin-right: 16rpx;
        flex-shrink: 0;
        display: flex;
        align-items: center;
        justify-content: center;
        flex-direction: column;
        &-text{
          font-size: 20rpx;
          color: #999;
          line-height: normal;
        }
        background: #E2E2E2;
      }
      .part-details {
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        flex: 1;
        .item-name {
          margin-bottom: 12rpx;
          font-size: 28rpx;
          color: #3D3D3D;
        }
        .part-info-row {
          font-size: 24rpx;
          display: flex;
          margin-bottom: 8rpx;
          &:last-child {
            margin-bottom: 0;
          }
          .label {
            white-space: nowrap;
            color: #999;
            margin-right: 10rpx;
          }
          .value {
            width: 0;
            flex: 1;
            color: #333;
          }
        }
      }
    }
  }
}
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 40rpx 0;
  image {
    width: 64rpx;
    height: 64rpx;
  }
  text {
    font-size: 26rpx;
    color: #999;
    margin-top: 20rpx;
  }
}

</style>